/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.stop.risk.alarm.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.stop.risk.alarm.dto.LineAlarmOverproofExcelDTO;
import com.hopes.bizz.stop.risk.alarm.entity.LineAlarmOverproofEntity;
import com.hopes.bizz.stop.risk.alarm.service.LineAlarmOverproofService;
import com.hopes.bizz.stop.risk.alarm.vo.LineAlarmOverproofExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 超标报警
 *
 * @author pig
 * @date 2024-02-22 15:21:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/overproof" )
@Tag(description = "overproof" , name = "超标报警管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LineAlarmOverproofController {

    private final LineAlarmOverproofService lineAlarmOverproofService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param lineAlarmOverproof 超标报警
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('alarm_overproof_view')" )
    public R getlineAlarmOverproofPage(Page page, LineAlarmOverproofEntity lineAlarmOverproof) {
        LambdaQueryWrapper<LineAlarmOverproofEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObjectUtil.isNotNull(lineAlarmOverproof.getMonitorSiteId()),LineAlarmOverproofEntity::getMonitorSiteId,lineAlarmOverproof.getMonitorSiteId());
		wrapper.eq(ObjectUtil.isNotNull(lineAlarmOverproof.getAlarmStatus()),LineAlarmOverproofEntity::getAlarmStatus,lineAlarmOverproof.getAlarmStatus());
		wrapper.eq(ObjectUtil.isNotNull(lineAlarmOverproof.getHandleStatus()),LineAlarmOverproofEntity::getHandleStatus,lineAlarmOverproof.getHandleStatus());
        return R.ok(lineAlarmOverproofService.page(page, wrapper));
    }


    /**
     * 通过id查询超标报警
     * @param  id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('alarm_overproof_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(lineAlarmOverproofService.getById(id));
    }

    /**
     * 新增超标报警
     * @param lineAlarmOverproof 超标报警
     * @return R
     */
    @Operation(summary = "新增超标报警" , description = "新增超标报警" )
    @SysLog("新增超标报警" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('alarm_overproof_add')" )
    public R save(@RequestBody LineAlarmOverproofEntity lineAlarmOverproof) {
        return R.ok(lineAlarmOverproofService.save(lineAlarmOverproof));
    }

    /**
     * 修改超标报警
     * @param lineAlarmOverproof 超标报警
     * @return R
     */
    @Operation(summary = "修改超标报警" , description = "修改超标报警" )
    @SysLog("修改超标报警" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('alarm_overproof_edit')" )
    public R updateById(@RequestBody LineAlarmOverproofEntity lineAlarmOverproof) {
        return R.ok(lineAlarmOverproofService.updateById(lineAlarmOverproof));
    }

    /**
     * 通过id删除超标报警
     * @param ids 列表
     * @return R
     */
    @Operation(summary = "通过id删除超标报警" , description = "通过id删除超标报警" )
    @SysLog("通过id删除超标报警" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('alarm_overproof_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(lineAlarmOverproofService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('alarm_overproof_export')")
    public List<LineAlarmOverproofExcelVO> template() {
        return lineAlarmOverproofService.exportExcel(Collections.singletonList(new LineAlarmOverproofEntity()));
    }

    /**
     * 导出excel 表格
     * @param lineAlarmOverproofExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('alarm_overproof_export')")
    public List<LineAlarmOverproofExcelVO> export(LineAlarmOverproofExcelDTO lineAlarmOverproofExcelDto) {
        return lineAlarmOverproofService.exportExcel(lineAlarmOverproofService.list());
    }

    /**
     * 导入excel 表格
     * @param lineAlarmOverproofExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('alarm_overproof_export')")
    public R importExcel(@RequestExcel List<LineAlarmOverproofExcelDTO> lineAlarmOverproofExcelDtoList, BindingResult bindingResult) {
        return lineAlarmOverproofService.importExcel(lineAlarmOverproofExcelDtoList, bindingResult);
    }
}
